package com.example.yls.demoa;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private ImageView iv_wifi;
    private ImageView iv_dog;
    private Button btnStart;
    private Button btnStop;
    private AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_wifi = findViewById(R.id.iv_wifi);
        iv_dog = findViewById(R.id.iv_dog);
        btnStart = findViewById(R.id.btn_start);
        btnStop = findViewById(R.id.btn_stop);

        animationDrawable = (AnimationDrawable) iv_wifi.getDrawable();

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //animationDrawable.start();
                //让狗狗淡入淡出（补间动画）
                //startAlphaAnim();
                //让狗狗平移
                //startTranslateAnim();
                //让狗狗缩放
                //startScaleAnim();
                //让狗狗旋转
                //startRotateAnim();
                //动画集合
                //startAnimationSetWithJava();
                startAnimationSetWithXML();
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                animationDrawable.stop();
            }
        });
    }

    private void startAnimationSetWithXML() {
        AnimationSet animationSet =
                (AnimationSet) AnimationUtils.loadAnimation(
                        MainActivity.this, R.anim.anim_set);
        iv_dog.startAnimation(animationSet);
    }

    private void startAnimationSetWithJava() {
        AnimationSet animationSet = new AnimationSet(true);
        TranslateAnimation translateAnimation =
                new TranslateAnimation(0, 200, 0, 100);
        ScaleAnimation scaleAnimation =
                new ScaleAnimation(1,3,1,3,180,180);
        RotateAnimation rotateAnimation =
                new RotateAnimation(0, 360, 120, 120);
        AlphaAnimation alphaAnimation =
                new AlphaAnimation(1.0f, 0.5f);
        animationSet.addAnimation(translateAnimation);
        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(rotateAnimation);
        animationSet.addAnimation(alphaAnimation);
        animationSet.setDuration(3000);
        animationSet.setFillAfter(true);
        animationSet.setRepeatMode(Animation.REVERSE);
        iv_dog.startAnimation(animationSet);
    }

    private void startRotateAnim() {
        /*
        RotateAnimation rotateAnimation =
                (RotateAnimation) AnimationUtils.loadAnimation(
                        MainActivity.this, R.anim.rotate_anim);
        */

        RotateAnimation rotateAnimation =
                new RotateAnimation(0, 360, 180, 180);
        rotateAnimation.setDuration(3000);
        rotateAnimation.setRepeatCount(RotateAnimation.INFINITE);
        rotateAnimation.setRepeatMode(RotateAnimation.REVERSE);
        iv_dog.startAnimation(rotateAnimation);
    }

    private void startScaleAnim() {
        /*
        ScaleAnimation scaleAnimation =
                (ScaleAnimation) AnimationUtils.loadAnimation(
                        MainActivity.this, R.anim.scale_anim);
        */

        ScaleAnimation scaleAnimation = new ScaleAnimation(
                0, 2, 0, 2, 100, 100);
        scaleAnimation.setDuration(3000);
        scaleAnimation.setFillAfter(true);
        scaleAnimation.setRepeatCount(ScaleAnimation.INFINITE);
        scaleAnimation.setRepeatMode(ScaleAnimation.REVERSE);
        iv_dog.startAnimation(scaleAnimation);
    }

    private void startTranslateAnim() {
        /*
        TranslateAnimation translateAnimation =
                (TranslateAnimation) AnimationUtils.loadAnimation(
                        MainActivity.this, R.anim.translate_anim);
        */

        TranslateAnimation translateAnimation =
                new TranslateAnimation(0.0f, 400.0f, 0.0f, 400.0f);
        translateAnimation.setDuration(3000);
        translateAnimation.setFillAfter(true);
        translateAnimation.setRepeatCount(TranslateAnimation.INFINITE);
        translateAnimation.setRepeatMode(TranslateAnimation.REVERSE);

        iv_dog.startAnimation(translateAnimation);
    }


    private void startAlphaAnim() {
        /*第一种 使用xml
        AlphaAnimation alphaAnimation =
                (AlphaAnimation) AnimationUtils.loadAnimation(
                        MainActivity.this, R.anim.alpha_anim);
        */

        //第二种 使用java代码
        //从不透明到透明显示
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setFillAfter(true); //保留最后的状态
        alphaAnimation.setRepeatCount(3); //重复3次
        alphaAnimation.setRepeatMode(AlphaAnimation.REVERSE); //反转模式
        iv_dog.startAnimation(alphaAnimation);
    }
}
